<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>药品盘点</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="renderer" content="webkit">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta content="telephone=no" name="format-detection">
    <meta name="x5-fullscreen" content="true">
    <meta name="full-screen" content="yes">
    <link rel="stylesheet" href="http://at.alicdn.com/t/font_363663_xo7863s0yfmbzkt9.css"
          th:href="@{http://at.alicdn.com/t/font_363663_xo7863s0yfmbzkt9.css}">
    <link rel="stylesheet" href="plugins/weui/weui.min.css" th:href="@{/static/plugins/weui/weui.min.css}">
    <link rel="stylesheet" href="plugins/jquery-weui/css/jquery-weui.css" th:href="@{/static/plugins/jquery-weui/css/jquery-weui.css}">
    <link rel="stylesheet" href="plugins/swiper/css/swiper.min.css" th:href="@{/static/plugins/swiper/css/swiper.min.css}">
    <link rel="stylesheet" href="css/style.css" th:href="@{/static/css/style.css}">
    <!--basis script-->
    <script src="plugins/adaptive.js/adaptive.js" th:src="@{/static/plugins/adaptive.js/adaptive.js}"></script>
    <script src="plugins/jquery/jquery.min.js" th:src="@{/static/plugins/jquery/jquery.min.js}"></script>
    <script src="plugins/weui/weui.js" th:src="@{/static/plugins/weui/weui.js}"></script>
    <script src="plugins/jquery-weui/js/jquery-weui.min.js"
            th:src="@{/static/plugins/jquery-weui/js/jquery-weui.min.js}"></script>
    <script src="plugins/fastclick/fastclick.js" th:src="@{/static/plugins/fastclick/fastclick.js}"></script>
    <script src="plugins/vue/vue.js" th:src="@{/static/plugins/vue/vue.js}"></script>
    <script src="plugins/vue/axios.min.js" th:src="@{/static/plugins/vue/axios.min.js}"></script>
    <script src="plugins/vue/ajax-tool.js" th:src="@{/static/plugins/vue/ajax-tool.js}"></script>
</head>
<body>
<div class="view">
    <div class="page" id="inventory-detail">
        <div class="weui-panel weui-panel_access info-panel">
            <div class="weui-panel__bd">
                <div class="weui-media-box weui-media-box_appmsg">
                    <div class="weui-media-box__bd">
                        <h4 class="weui-media-box__title">{{druginfo.gn}} | {{druginfo.wn}}</h4>
                        <p class="desc">规格：{{druginfo.spr}}</p>
                        <p class="desc">产地：{{druginfo.made}}</p>
                        <p class="desc">单位：{{druginfo.utr}}</p>
                    </div>
                </div>
            </div>
        </div>
        <div class="weui-cells weui-cells_form">
            <div class="weui-cell weui-cell_switch">
                <div class="weui-cell__bd">隐藏零库存</div>
                <div class="weui-cell__ft">
                    <input class="weui-switch" id="zero" type="checkbox" checked @click="zerocg"/>
                </div>
            </div>
        </div>
        <div class="weui-panel weui-panel_access my-panel" v-for="drugdetail in drugdetails">
            <div class="weui-form-preview my-preview">
                <div class="weui-form-preview__bd">
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">批号</label>
                        <span class="weui-form-preview__value">{{drugdetail.bn}}</span>
                    </div>
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">进价</label>
                        <span class="weui-form-preview__value">￥{{drugdetail.pri}}</span>
                    </div>
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">售价</label>
                        <span class="weui-form-preview__value">￥{{drugdetail.prr}}</span>
                    </div>
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">有效期</label>
                        <span class="weui-form-preview__value">{{drugdetail.vdate}}</span>
                    </div>
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">系统库存</label>
                        <span class="weui-form-preview__value">{{drugdetail.quan}}</span>
                    </div>
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">清点库存</label>
                        <input class="weui-input" type="number" v-model="drugdetail.realquan"
                               placeholder="请输入实际库存数量">
                    </div>
                    </div>
                </div>
            </div>
        <p class="weui-btn-area">
            <a class="weui-btn weui-btn_primary" href="#" @click="savestk" >确认</a>
        </p>
        </div>

    </div>
</div>
<!--scripts-->
<script src="js/main.js"></script>
<script>
    $(function () {
        var inventorydetail = new Vue({
            el: '#inventory-detail',
            data: {
                ischange: false,
                zero: false
            },
            computed: {
                druginfo: function(){
                    return JSON.parse(sessionStorage.getItem('drugstk'));
                },
                drugdetails: function () {
                    if(this.ischange){
                        console.info('一旦变化 重新刷新列表')
                    }
                    let drugstk = JSON.parse(sessionStorage.getItem('drugstk'))
                    //TODO 查询药品不同批号的数量
                    let res = []
                    ajaxsync({
                        url: '/mph/mr/stkph?aid='+drugstk.aid+'&mid='+drugstk.mid+'&zero='+this.zero,
                        method: 'get',
                        datajson: null,
                        succ: function (data) {
                            res = data.stkdetail
                        }
                    })
                    return res
                }
            },
            methods: {
                savestk(){
                    console.info('开始盘点')
                    let pyjson = {
                        aid: this.druginfo.aid,
                        mid: this.druginfo.mid,
                        gn: this.druginfo.gn,
                        wn: this.druginfo.wn,
                        made: this.druginfo.made,
                        spr: this.druginfo.spr,
                        utr: this.druginfo.utr,
                        typ: 1,
                        vwMrStks: []
                    }
                    let pkjson = {
                        aid: this.druginfo.aid,
                        mid: this.druginfo.mid,
                        gn: this.druginfo.gn,
                        wn: this.druginfo.wn,
                        made: this.druginfo.made,
                        spr: this.druginfo.spr,
                        utr: this.druginfo.utr,
                        typ: 2,
                        vwMrStks: []
                    }
                    for(let i=0; i<this.drugdetails.length; i++){
                        let items = this.drugdetails[i]
                        if(items.quan < items.realquan){
                            pyjson.vwMrStks.push(items)
                        }
                        if(items.quan > items.realquan){
                            pkjson.vwMrStks.push(items)
                        }
                    }
                    let paramjson = [pyjson, pkjson]
            //        console.info(paramjson)
            //        return false
                    let _this = this
                    ajaxsync({
                        url: '/mph/mr/savestk',
                        method: 'post',
                        datajson: paramjson,
                        succ: function (data) {
             //               console.info("盘点成功")
                            $.toast("盘点成功");
                            _this.ischange = !_this.ischange
                        }
                    })
                },
                zerocg(){
                    this.zero = !this.zero
                    this.ischange = !this.ischange
                }
            }
        })
    })
</script>
</body>
</html>
